//
//  SACustomerDetailViewController.m
//  SAP
//
//  Created by 中矿科技有限公司 on 17/9/16.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "SACustomerDetailViewController.h"
#import "SAContractSearchViewController.h"

#import "SACustomerDetailCell.h"
#import "SACustomerDetailModel.h"

@interface SACustomerDetailViewController ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *customerArray;

@end
static NSString *const cellID =@"SACustomerDetailCell";
@implementation SACustomerDetailViewController
{
    AFHTTPData *_http;
    
    MBProgressHUD *_waitHud;
    SACustomerDetailModel *_sourceModel;//详情数据
    NSDictionary *_detailDict;

}

-(UITableView *)tableView{
    if (_tableView) {
        return _tableView;
    }
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"SACustomerDetailCell" bundle:nil] forCellReuseIdentifier:cellID];
    _tableView.showsVerticalScrollIndicator =NO;
    _tableView.showsHorizontalScrollIndicator =NO;
    _tableView.delegate =self;
    _tableView.dataSource =self;
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _http =[[AFHTTPData alloc]init];
    _customerArray =@[@[@"客户编号",@"客户名称",@"地址",@"法定代表人",@"委托代表人",@"合同数"],@[@"开户银行",@"银行账号",@"纳税登记号",@"邮政编号"],@[@"联系电话",@"重点客户"]];
    [self customUI];
    [self prepareData];
}
-(void)customUI{
    self.view.backgroundColor =SA_AllBackColor;
    self.navigationItem.title =@"客户详情";
    
    [self.view addSubview:self.tableView];
}

-(void)prepareData{
    if(!_waitHud)_waitHud=[[MBProgressHUD alloc] initWithView:self.view];//加载
    [self.view addSubview:_waitHud];
    // _waitHud.mode = MBProgressHUDModeText;
    [_waitHud showAnimated:YES];
    NSString *strUrl =[NSString stringWithFormat:SAPCompanyDetailKUrl,self.customerId];
    [_http getDataWithUrl:strUrl onSuccess:^(NSData *data) {
        
       // _detailDict =[ConstractDetailModel getGetOptionsDetailDictfromData:data];
        
        _sourceModel =[SACustomerDetailModel getCustomerDetailfromData:data];
        

        [_tableView reloadData];
        [_waitHud showAnimated:NO];
        [_waitHud hideAnimated:YES];
    } onFailure:^(NSError *error) {
        [_waitHud showAnimated:NO];
        [_waitHud hideAnimated:YES];
    }];
}


#pragma mark tableView-Delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _customerArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  [_customerArray[section] count];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 49;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20.f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1f;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SACustomerDetailCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
   if(indexPath.row ==5) cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailName.text =_customerArray[indexPath.section][indexPath.row];
    //基本信息
    [cell setFromCustomerDetailModel:_sourceModel indexPath:indexPath];

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==5 && _sourceModel.nowContractConut.integerValue >0) {
        SAContractSearchViewController *saContractSVC  =[[SAContractSearchViewController alloc]init];
        saContractSVC.buyer =_sourceModel.Id;
        [self.navigationController pushViewController:saContractSVC animated:NO];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
